//
//  MXHomeListController.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/12.
//

#import "MXHomeListController.h"
#import "MxPublicNav.h"
#import "MXMineHomeListCell.h"
#import "MXHomeModel.h"
#import "MXMyHomeViewController.h"
@interface MXHomeListController () <MxNavigationViewDelegate,UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView         *listTab;
@property (nonatomic, strong) MxPublicNav *nav;
@property (nonatomic, strong) NSMutableArray *listArray;
@end

@implementation MXHomeListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F6F7F9"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadList) name:@"updatehomename" object:nil];
    self.listArray = [[NSMutableArray alloc] init];
    [self createUI];
    [self getUserHomeInfo];
}


- (void)createUI {
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.titleLab.text = @"家庭列表";
    self.nav.delegate = self;
    [self.view addSubview:self.nav];
    
    [self.view addSubview:self.listTab];
    [self.listTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.offset(kNavBarHeight);
    }];
}

- (void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self dc_navBarHidden:YES];
}

#pragma mark - ________ UITableView ________

- (UITableView *)listTab {
    
    if (nil == _listTab) {
        _listTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listTab.backgroundColor = [UIColor colorWithHexString:@"#F6F7F9"];
        _listTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTab.delegate = self;
        _listTab.dataSource = self;
        _listTab.rowHeight = 83.0;
        _listTab.showsVerticalScrollIndicator = NO;
        _listTab.showsHorizontalScrollIndicator= NO;
        [_listTab registerClass:[MXMineHomeListCell class] forCellReuseIdentifier:NSStringFromClass([MXMineHomeListCell class])];
        if (@available(iOS 11.0, *)) {
            _listTab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _listTab.sectionHeaderTopPadding = 0;
        }
    }
    return _listTab;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MXHomeModel *model = self.listArray[indexPath.row];
    MXMineHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MXMineHomeListCell class])];
    [cell reloadCellWithModel:model];
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MXHomeModel *model = self.listArray[indexPath.row];
    MXMyHomeViewController *vc = [[MXMyHomeViewController alloc] init];
    vc.homeModel = model;
    vc.count = model.deviceList.count;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)addHome {
    
}

- (void)reloadList {
//    self.page = 1;
    [self getUserHomeInfo];
}

//获取用户家庭信息
- (void)getUserHomeInfo {
    MJWeakSelf
    [self.view makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:@"api/app/appUser/userFamily" params:@{} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSDictionary * dict = [responseObject mj_JSONObject];
        [weakSelf.view hideToastActivity];
        if (dict[DC_ResultCode_Key] && [dict[DC_ResultCode_Key] integerValue] == DC_Result_Success) {
            [weakSelf.listArray removeAllObjects];
            NSArray *dataArray =  [NSArray modelArrayWithClass:[MXHomeModel class] json:dict[@"data"]];
            [weakSelf.listArray addObjectsFromArray:dataArray];
            [weakSelf.listTab reloadData];
            
        } else {

            [weakSelf.view xjmakeToast:dict[DC_ResultMsg_Key]];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
